<?php
namespace control;
/*
*图片设置类
*/

class imgClass extends adIndexClass
{
	/*列表页-查询条件*/
	public function home()
	{
		$html =
			text(array("name" => "id", "title" => "{l.img.home.name}")) .
			repeatSelect(array("sqlForm" => "img", "sqlField" => "type", "name" => "type", "title" => "{l.img.home.type}")) .
			text(array("name" => "name", "title" => "{l.img.home.name}")) .
			text(array("name" => "src", "title" => "{l.img.home.src}"));
		//返回
		return $this->homeFun($this->search($html));
	}
	
	/*系统设置-图片设置~列表页~查询结果*/
	public function homeSearch()
	{
		//like查询数组
		$like = array(
			"name" => $this->post('name'),//图片名称
			"src"  => $this->post('src'),//相对路径
		);
		//全等查询数组
		$equal = array(
			"id"   => $this->post('id'),//图片ID号
			"type" => $this->post('type'),//分类
		);
		//获取查询结果
		$where = search(array("like" => $like, "=" => $equal));
		$page  = $this->page(array("table" => "img", "where" => $where, "order" => "time desc"));
		//获取表头
		$checkAll = $this->checkAll("listForm", "adImgId");
		// $tr[]     = array($checkAll, "ID号", "分类", "名称", "格式", "限宽", "限高", "体积", "排序", "更新时间");
		$tr[] = [$checkAll, "{l.img.homeSearch.id}", "{l.img.homeSearch.type}", "{l.img.homeSearch.name}", "{l.img.homeSearch.geshi}", "{l.img.homeSearch.width}", "{l.img.homeSearch.height}", "{l.img.homeSearch.size}", "{l.img.homeSearch.list}", "{l.img.homeSearch.updateTime}"];
		//获取记录
		foreach ($page['array'] as $array) {
			$size = $array['maxSize'] . "KB";
			//列表控件
			$dominate = $this->dominate(array(
				"box"    => array("name" => "adImgId", "value" => $array['id']),
				"upload" => array("title" => "更新图片", "click" => "icoFun('imgForm','" . $array['id'] . "')"),
				"img"    => array("title" => "点击查看大图", "href" => $array['src']),
			));
			$tr[]     = array($dominate, $array['id'], $array['type'], $array['name'], $array['geshi'], $array['width'], $array['height'], $size, $array['list'], $array['updateTime']);
		}
		//控件
		$button = "
		<div class='hide'>
			<form name='imgForm' action='" . root . "control/adIndex.html?cla=img&fun=newImg' method='post' enctype='multipart/form-data'>
				<input name='id' type='hidden'>
				<input name='file' type='file' onchange='document.imgForm.submit()'>
			</form>
		</div>
		";
		//返回
		$json['html'] = $page['hint'] . $button . "<form name='listForm'>" . space . $this->tableMany(array("tr" => $tr)) . "</form>" . $page['button'];
		return json_encode($json, 320);
	}
	
	/*系统设置-图片设置-更新*/
	public function newImg()
	{
		//赋值
		$id  = $this->post('id');//员工ID号
		$img = query("img", " id = '$id' ");
		//判断
		if (!$this->power("img", "edit")) {
			$warn = lang('img.newImg.powerMsg');
			// $warn = "你没有系统图片的编辑权限";
		} elseif (empty($id)) {
			$warn = lang('img.newImg.idNone');
			// $warn = "图片ID号为空";
		} elseif (empty($img['id'])) {
			$warn = lang('img.newImg.imgNone');
			// $warn = "未找到此图片";
		} else {
			$para = array(
				"fileName"  => "file",//上传图像的表单文件域名称
				"width"     => $img['width'],//图像宽度，一旦设定，图像必须按照此宽度上传
				"height"    => $img['height'],//图像高度，一旦设定，图像必须按照此高度上传
				"maxHeight" => "",//图像最大高度，超高图片切片时只设定每张图片的最大高度，不限定图像高度
				"maxSize"   => $img['maxSize'],//图像最大体积KB
				"type"      => "更新图像",//插入类型：更新图像、新增图像
				"sqlSearch" => " select * from img where id = '$id' ",//查询图片的数据库代码
				"maxNum"    => "",//新增图像时限定的图像总数
				"field"     => "src",//存放图片根目录的数据库字段名
				"url"       => "img/websiteImg/" . $id . ".jpg",//新图片保存的网站根目录位置
			);
			//数据库图片处理参数，如果是新增则是函数insert的参数，如果是更新则是函数update的参数
			$para['sqlIn'] = array("table" => "img", "field" => array($para['field'] => $para['url'], "updateTime" => date("Y-m-d H:i:s")), "where" => " id = '$id' ");
			$warn          = imgUpdateCheck($para);
			//添加日志
			if ($warn == "图像处理成功") {
				$text = $this->control['name'] . "更新了系统图片：" . $img['type'] . "-" . $img['name'];
				$this->logText(array("type" => "img", "text" => $text));
			}
		}
		$_SESSION['warn'] = $warn;
		header("location:" . getenv("HTTP_REFERER"));
		exit(0);
	}
}

